<%@page import="com.jfinal.kit.PropKit" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" import="cn.com.dashihui.web.common.OrderCode" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <title>退款管理</title>
    <jsp:include page="../../include/header.jsp"></jsp:include>
    <style type="text/css">
			table th{
						text-align: center;
			            white-space: nowrap;
			        }
			table td{
						text-align: center;
			            white-space: nowrap;
			        }
			  table{
			         empty-cells:show; 
			         border-collapse: collapse;
			         margin:0 auto;
			        }
					 	  /*排序 */
			.querySort{
			   position:relative;
			}
			.down{
			   position: absolute;
			   top:12px;
			   display:inline-block;
			   width:10px;
			   height:12px;
			   margin-left: 8px;
			   background: url("${BASE_PATH}/static/images/sprite-arrow.png") 0 61px;
			}
		  .sort .querySort:NTH-OF-TYPE(2) .down{
			    top:-3px;
			    transform: rotateX(180deg);
			}
			.querySort:hover .down{
			   background: url("${BASE_PATH}/static/images/sprite-arrow.png") 0 21px;
			}
			
		  .downSort{
			    position: absolute;
			    top: 3px;
			    left: 3px;
			    width: 18px;
			    height: 24px;
			    opacity: 0;
			    cursor:pointer;
			}
		    .upSort {
			    position: absolute;
			    top: -15px;
			    left: 3px;
			    width: 18px;
			    height: 24px;
			    opacity: 0; 
			    cursor:pointer;
			}

            
		</style>
</head>
<body>
<div id="wrapper"  class="row">
	
			<div class="col-lg-12">
				<h1 class="page-header">退款</h1>
			</div>
		<!-- 搜索框 -->
			<div class="title-top col-lg-12">
				<label class="search-label">订单编号：</label><input type="text" id="sOrderNum" value="" class="form-control search-input width200" maxlength="21">
				<label class="search-label">买家电话：</label><input type="text" id="sTel" value="" class="form-control search-input width200" maxlength="13">
				<label class="search-label">买家姓名：</label><input type="text" id="sName" value="" class="form-control search-input width200" maxlength="13">
				<label class="search-label">退款状态：</label>
				<div class="search-input">
					<select id="sTuiState" class="selectpicker" data-width="200">
						<option value="0">全部</option>
						<option value="1">审核中</option>
						<option value="2">审核通过</option>
						<option value="3">驳回</option>
						<option value="4">退款完成</option>
						<option value="5">退款异常</option>
					</select>
				</div>
			</div>
			<div class=" col-lg-12 title-top">
				<button class="btn btn-success search-btn" onclick="query();">确定</button>
				<button class="btn btn-success search-btn" onclick="reset();">重置</button>
				<button class="btn btn-default search-btn" title="刷新" onclick="window.location.reload();"> 刷新</button>
			</div>

			<div class="col-lg-12 title-top">
		<div class="table-responsive panel panel-default" >
			<table id="dataTable" class="table table-hover table-bordered" style="overflow:scroll;">
	            <thead>
	               	<tr>
						<th width="100px;">状态</th>
						<th width="140px;">退款</th>
						<th width="200px;">订单号</th>
						<th width="120px;">会员姓名</th>
						<th width="160px;">会员手机号</th>
						<th width="200px;">下单公司</th>
						<th width="200px;">下单时间</th>
						<th width="80px;">订单总价值</th>
						<th width="200px">退款原因</th>
					</tr>
	            </thead>
	            <tbody id="dataList"></tbody>
	        </table>
		</div>
        	<div class="col-lg-12">
        		<div class = "pull-right">
        			<div class="text-muted  pull-left" style="margin-top:20px; margin-right:18px;">
					<select id="pageSize" name="pageSize" class="selectpicker" data-width="100" onchange="query()" >
    					<option value="10" selected = "selected">10条/页</option>
						<option value="15">15条/页</option>
						<option value="20">20条/页</option>
						<option value="50">50条/页</option>
						<option value="100">100条/页</option>
    				</select>
					</div>
        			<ul id="dataPagination" class="pagination-sm pull-right"></ul>
        		</div>
			</div>
		</div>
	</div>
<jsp:include page="../../include/javascripts.jsp"></jsp:include>
<!-- 异步加载下一页数据后，用模板渲染 -->
<script type="text/html" id="dataTpl">
		{{each list as item}}
				 <tr id="item{{item.orderNum}}" data-id="{{item.orderNum}}">
					{{if item.orderState == 1}}
						<td>待付款</td>
					{{else if item.orderState == 2}}
						<td>待出库</td>
					{{else if item.orderState == 3}}
						<td>待评价</td>
					{{else if item.orderState == 4}}
						<td>已完成</td>
					{{else if item.orderState == 5}}
						<td>删除</td>	
					{{else if item.orderState == 7}}
						<td>退款完成</td>
					{{else if item.orderState == 9}}
						<td>过期订单</td>	
					{{else if item.orderState == 6}}
						<td>待收货</td>
					{{/if}}
					{{if item.tuistate == 0}}
							<td>正常</td>
						{{else if item.tuistate == 1}}
						<td>
							<a href="javascript:doAccept('{{item.orderNum}}');"><span class="btn">通过</span></a>
							<a href="javascript:toCancel('{{item.orderNum}}');"><span class="btn">驳回</span></a>
						</td>
						{{else if item.tuistate == 2}}
							<td>已通过</td>
						{{else if item.tuistate == 3}}
							<td>已驳回</td>
						{{else if item.tuistate == 4}}
							<td>已退款</td>	
						{{else if item.tuistate == 5}}
							<td>退款异常</td>	
					{{/if}}
					<td><a href="javascript:showOrder('{{item.orderNum}}');"><font color="#40a9ff" ="dataCount">{{item.orderNum}}</font></a></td>
					<td><a href="javascript:showUser('{{item.uid}}');"><font color="#40a9ff" id="dataCount">{{item.name}}</font></a></td>
					<td>{{item.phone}}</td>
					<td>{{item.sellerName}}</td>
					<td>{{item.startDate}}</td>
					<td>{{item.amount}}</td>
					<td>{{item.refundReason}}
						{{if item.refundReason == null}}--{{/if}}
					</td>
				</tr>
		{{/each}}
</script>
<script type="text/javascript">
var dataPaginator;
var OrderNum="${OrderNum}";
var userId="${userId}";
$(query);
function query(){
	var pageSize=$("#pageSize").val();
	var  sSellerId = $("#sSellerId").val(),sStoreId = $("#sStoreId").val();
	var sell,store;
	if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
		sell=sSellerId.join("','");
	}
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	var params = {
		pageSize:pageSize,
		orderNum:$("#sOrderNum").val(),
		tel:$("#sTel").val(),
		name:$("#sName").val(),
		tuiState:$("#sTuiState").val(),
	};
	if(dataPaginator){
		dataPaginator.destroy();
	}
	if(!userId){
		dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/tui",params,function(result){
			//设置显示最新的数据数量
			$("#dataCount").html(result.object.totalRow);
			//根据模板渲染数据并填充
			$("#dataList").empty().append(template("dataTpl",result.object));
			findTotal();
		});
		
	}else{
		//根据用户Id查询此用户所有订单
		dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/doConvert",{userId:userId},function(result){
			//设置显示最新的数据数量
			$("#dataCount").html(result.object.totalRow);
			//根据模板渲染数据并填充
			$("#dataList").empty().append(template("dataTpl",result.object));
			findTotal(userId);
		});
	}
	
}
//显示会员详细信息
function showUser(id){
	if(id){
		showDialog = Kit.dialog("会员详情信息","${BASE_PATH}/bus/order/userdetail/"+id,{size:'size-wide',closable:true}).open();
	}else{
		//根据用户Id查询此用户所有订单
		showDialog = Kit.dialog("会员详情信息","${BASE_PATH}/bus/order/userdetail/"+userId,{size:'size-wide',closable:true}).open();
	}
}
//显示拓客详细信息
var showTokerDialog;
function showToker(tokerid){
	showTokerDialog = Kit.dialog("查看拓客信息","${BASE_PATH}/bus/order/showToker?tokerid="+tokerid,{size:'size-wide',closable:true}).open();
}
//显示订单详细信息

var showOrderDialog;
function showOrder(orderNum){
	showOrderDialog = Kit.dialog("查看订单","${BASE_PATH}/bus/order/detail?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
/**
 * 此排序与默认排序冲突故分成二方法
 */
 function query1(index){
	   findTotal();
	   var pageSize=$("#pageSize").val();
		var  sSellerId = $("#sSellerId").val(),sStoreId = $("#sStoreId").val();
		var sell,store;
		if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
			sell=sSellerId.join("','");
		}
		if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
			store=sStoreId.join("','");
		}
		var params = {
			pageSize:pageSize,
			orderNum:$("#sOrderNum").val(),
			beginDate:$("#sBeginDate").val(),
			endDate:$("#sEndDate").val(),
			tel:$("#sTel").val(),
			state:$("#sState").val(),
			sellerId:sell,
			storeId:store,
			index:index
		};
		if(dataPaginator){
			dataPaginator.destroy();
		}
		dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/page1",params,function(result){
			//设置显示最新的数据数量
			$("#dataCount").html(result.object.totalRow);
			//根据模板渲染数据并填充
			$("#dataList").empty().append(template("dataTpl",result.object));
			
		});
	}
function reset(){
	$("#sOrderNum").val("");$("#sBeginDate").val("");$("#sEndDate").val("");$("#sTel").val("");
	$("#sSellerId").val("");$("#sSellerId").trigger("change");  $("#sStoreId").val("");$("#sStoreId").trigger("change");  $("#sState").val("0");$("#sState").trigger("change");
	$("#sStoreId").selectpicker("refresh"); $("#sSellerId").selectpicker("refresh");
}
function doExcel(){
	var orderNum=$("#sOrderNum").val(),beginDate=$("#sBeginDate").val(), endDate=$("#sEndDate").val(),tel=$("#sTel").val(),state=$("#sState").val(),sSellerId=$("#sSellerId").val(),sStoreId=$("#sStoreId").val();
	var sell="",store="";
	if(sSellerId !=null && sSellerId !="" && sSellerId !=undefined){
		sell=sSellerId.join("','");
	}
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	if(!userId){
		window.location.href="${BASE_PATH}/bus/order/doExcel?orderNum="+orderNum+"&beginDate="+beginDate+"&endDate="+endDate+"&tel="+tel+"&state="+state+"&sellerId="+sell+"&storeId="+store;
	}else{
		//根据用户ID导出
		window.location.href="${BASE_PATH}/bus/order/doConvertExcel?userId="+userId;

	}
}
$("#sSellerId").change(function(){
	 var sSellerId=[];
	  sSellerId=$("#sSellerId").val();
	  $.ajax({
               type: "POST",
               url: "${BASE_PATH}/bus/goods/base/chooseStore",
               data: {"sSellerId" : sSellerId},
               dataType: "json",
               traditional: true,
               success: function(data){
              	 $("#sStoreId").empty();
                         for(var i=0; i<data.object.length;i++){
                        	 $("#sStoreId").append("<option value='"+data.object[i].id+"'>"+data.object[i].title+"</option>");
                         }
                         $("#sStoreId").selectpicker("refresh");
                     }
           });
      }); 
$("#sBeginDate").datetimepicker({
    format: 'yyyy-mm-dd hh:ii:ss',
    minuteStep:1,
    minView:'hour',
    language: 'zh-CN',
    pickerPosition:'bottom-right',
    autoclose:true,
}).on("click",function(){
	var d2=new Date();
	var d=$("#sEndDate").val();
	if(d==""){
		 $("#sBeginDate").datetimepicker("setEndDate",d2);
	}else{
		var d1=new Date(d); 
		d1.setDate(d1.getDate()-1);
		if(d1>d2){
	  	 	 $("#sBeginDate").datetimepicker("setEndDate",d2);
		}else{
			 $("#sBeginDate").datetimepicker("setEndDate",d1);
		}
	}
});

$("#sEndDate").datetimepicker({
    format: 'yyyy-mm-dd hh:ii:ss',
    minuteStep:1,
    minView:'hour',
    language: 'zh-CN',
    autoclose:true,
	}).on("click",function(){
		var d=$("#sBeginDate").val();
		var d1=new Date(d); 
		d1.setDate(d1.getDate()+1); 
		$("#sEndDate").datetimepicker("setStartDate",d1);
	});
	
 $(document).on("click","#sEndDate",function(){
    	$('#sEndDate').datetimepicker('show');
    });

$(document).on("click","#sBeginDate",function(){
	$('#sBeginDate').datetimepicker('show');
});

/**
 * 统计总所需积分和总现金价格
 */
function findTotal(){
	var sStoreId = $("#sStoreId").val();
	var sStoreId = $("#sStoreId").val();
	var store;
	if(sStoreId !=null && sStoreId !="" && sStoreId !=undefined){
		store=sStoreId.join("','");
	}
	var params = {
		pageSize:10,
		beginDate:$("#sBeginDate").val(),
		endDate:$("#sEndDate").val(),
		storeid:store,
		orderNum:$("#sOrderNum").val(),
		tel:$("#sTel").val(),
		state:$("#sState").val(),
		userId:userId
	};
	$.post("${BASE_PATH}/bus/order/findTotal",params,function(result){
		if(result.flag==0){
			//$("#payPoint").text(result.object.payPoint);//总所需积分
			$("#amount").text(result.object.amount.toFixed(2));//总累计现金
			$("#tuikuan").text(result.object.tuikuan1.toFixed(2));//总退款现金
			$("#shiji").text(result.object.shiji.toFixed(2));//总实际现金
		}else{
			Kit.alert("操作失败");return;
		}
	});
}

//兑换有效期”添加排序（可按时间大到小／小到大排序） 单选图片点击切换
var querySort=document.getElementsByClassName("querySort");//点击区域
var down=document.getElementsByClassName("down");
for(var i=0;i<querySort.length;i++){
	   //给每个class添加id标识(当索引)区分每个class
	   querySort[i].id=i;	   
	   //鼠标点击改变背景图片偏移
	   querySort[i].onclick=function(){
		   //当前索引
		   var index=this.id;
		   for( j=0;j<down.length;j++){
			   down[j].style.backgroundPosition="0px 61px";
		   }
		 //改变索引为当前的class背景图片偏移位置
		   down[index].style.backgroundPosition="0px 21px";
	   }
}
function onEditSuccess(newObject){
	$("#item"+newObject.id).replaceWith(template("dataTpl",{"list":[newObject]}));
} 
//通过退款申请
function doAccept(orderNum){
	Kit.confirm("提示","确定要审核退款申请吗？",function(ref){
		$.post("${BASE_PATH}/bus/order/doAccept",{'orderNum':orderNum},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else{
				Kit.alert("操作失败");return;
			}
		});
	});
}
//驳回申请
var toCancelDialog;
function toCancel(orderNum){
	toCancelDialog = Kit.dialog("驳回原因","${BASE_PATH}/bus/order/toRefound?orderNum="+orderNum,{closable:true,size:"size-wide:100px"}).open();
}
</script>
</body>
</html>